package com.ruoyi.flowable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.flowable.domain.ProcessQuery;
import com.ruoyi.flowable.domain.bo.WfCopyBo;
import com.ruoyi.flowable.domain.vo.*;
import com.ruoyi.flowable.service.IWfCopyService;
import com.ruoyi.flowable.service.IWfProcessService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 工作流流程管理
 *
 * @author KonBAI
 * @createTime 2022/3/24 18:54
 */
@Tag(name = "工作流流程管理")
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/process")
public class WfProcessController extends BaseController {

    private final IWfProcessService processService;
    private final IWfCopyService copyService;

    /**
     * 查询可发起流程列表
     *
     * @param processQuery 参数
     */
    @Operation(summary = "查询可发起流程列表")
    @GetMapping(value = "/list")
    @SaCheckPermission("workflow:process:startList")
    public TableDataInfo<WfDefinitionVo> startProcessList(@ParameterObject ProcessQuery processQuery) {
        return processService.selectPageStartProcessList(processQuery);
    }

    /**
     * 我拥有的流程
     */
    @Operation(summary = "我拥有的流程")
    @SaCheckPermission("workflow:process:ownList")
    @GetMapping(value = "/ownList")
    public TableDataInfo<WfTaskVo> ownProcessList(@ParameterObject ProcessQuery processQuery) {
        return processService.selectPageOwnProcessList(processQuery);
    }

    /**
     * 获取待办列表
     */
    @Operation(summary = "获取待办列表")
    @SaCheckPermission("workflow:process:todoList")
    @GetMapping(value = "/todoList")
    public TableDataInfo<WfTaskVo> todoProcessList(@ParameterObject ProcessQuery processQuery) {
        return processService.selectPageTodoProcessList(processQuery);
    }

    /**
     * 获取待签列表
     *
     * @param processQuery 流程业务对象
     */
    @Operation(summary = "获取待签列表")
    @SaCheckPermission("workflow:process:claimList")
    @GetMapping(value = "/claimList")
    public TableDataInfo<WfTaskVo> claimProcessList(@ParameterObject ProcessQuery processQuery) {
        return processService.selectPageClaimProcessList(processQuery);
    }

    /**
     * 获取已办列表
     *
     * @param processQuery 参数
     */
    @Operation(summary = "获取已办列表")
    @SaCheckPermission("workflow:process:finishedList")
    @GetMapping(value = "/finishedList")
    public TableDataInfo<WfTaskVo> finishedProcessList(@ParameterObject ProcessQuery processQuery) {
        return processService.selectPageFinishedProcessList(processQuery);
    }

    /**
     * 获取抄送列表
     *
     * @param copyBo 流程抄送对象
     */
    @Operation(summary = "获取抄送列表")
    @SaCheckPermission("workflow:process:copyList")
    @GetMapping(value = "/copyList")
    public TableDataInfo<WfCopyVo> copyProcessList(@ParameterObject WfCopyBo copyBo) {
        startPage();
        copyBo.setUserId(getUserId());
        List<WfCopyVo> list = copyService.selectList(copyBo);
        return getDataTable(list);
    }

    /**
     * 导出可发起流程列表
     */
    @Operation(summary = "导出可发起流程列表")
    @SaCheckPermission("workflow:process:startExport")
    @Log(title = "可发起流程", businessType = BusinessType.EXPORT)
    @PostMapping("/startExport")
    public void startExport(@Validated ProcessQuery processQuery, HttpServletResponse response) {
        TableDataInfo<WfDefinitionVo> tableDataInfo = processService.selectPageStartProcessList(processQuery);
        ExcelUtil<WfDefinitionVo> util = new ExcelUtil<>(WfDefinitionVo.class);
        util.exportExcel(response, tableDataInfo.getRows(), "可发起流程");
    }

    /**
     * 导出我拥有流程列表
     */
    @Operation(summary = "导出我拥有流程列表")
    @SaCheckPermission("workflow:process:ownExport")
    @Log(title = "我拥有流程", businessType = BusinessType.EXPORT)
    @PostMapping("/ownExport")
    public void ownExport(@Validated ProcessQuery processQuery, HttpServletResponse response) {
        TableDataInfo<WfTaskVo> tableDataInfo = processService.selectPageOwnProcessList(processQuery);
        List<WfOwnTaskExportVo> listVo = BeanUtil.copyToList(tableDataInfo.getRows(), WfOwnTaskExportVo.class);
        for (WfOwnTaskExportVo exportVo : listVo) {
            exportVo.setStatus(ObjectUtil.isNull(exportVo.getFinishTime()) ? "进行中" : "已完成");
        }
        ExcelUtil<WfOwnTaskExportVo> util = new ExcelUtil<>(WfOwnTaskExportVo.class);
        util.exportExcel(response, listVo, "我拥有流程");
    }

    /**
     * 导出待办流程列表
     */
    @Operation(summary = "导出待办流程列表")
    @SaCheckPermission("workflow:process:todoExport")
    @Log(title = "待办流程", businessType = BusinessType.EXPORT)
    @PostMapping("/todoExport")
    public void todoExport(@Validated ProcessQuery processQuery, HttpServletResponse response) {
        TableDataInfo<WfTaskVo> tableDataInfo = processService.selectPageTodoProcessList(processQuery);
        List<WfTodoTaskExportVo> listVo = BeanUtil.copyToList(tableDataInfo.getRows(), WfTodoTaskExportVo.class);
        ExcelUtil<WfTodoTaskExportVo> util = new ExcelUtil<>(WfTodoTaskExportVo.class);
        util.exportExcel(response, listVo, "待办流程");
    }

    /**
     * 导出待签流程列表
     */
    @Operation(summary = "导出待签流程列表")
    @SaCheckPermission("workflow:process:claimExport")
    @Log(title = "待签流程", businessType = BusinessType.EXPORT)
    @PostMapping("/claimExport")
    public void claimExport(@Validated ProcessQuery processQuery, HttpServletResponse response) {
        TableDataInfo<WfTaskVo> tableDataInfo = processService.selectPageClaimProcessList(processQuery);
        List<WfClaimTaskExportVo> listVo = BeanUtil.copyToList(tableDataInfo.getRows(), WfClaimTaskExportVo.class);
        ExcelUtil<WfClaimTaskExportVo> util = new ExcelUtil<>(WfClaimTaskExportVo.class);
        util.exportExcel(response, listVo, "待签流程");
    }

    /**
     * 导出已办流程列表
     */
    @Operation(summary = "导出已办流程列表")
    @SaCheckPermission("workflow:process:finishedExport")
    @Log(title = "已办流程", businessType = BusinessType.EXPORT)
    @PostMapping("/finishedExport")
    public void finishedExport(@Validated ProcessQuery processQuery, HttpServletResponse response) {
        TableDataInfo<WfTaskVo> tableDataInfo = processService.selectPageFinishedProcessList(processQuery);
        List<WfFinishedTaskExportVo> listVo = BeanUtil.copyToList(tableDataInfo.getRows(), WfFinishedTaskExportVo.class);
        ExcelUtil<WfFinishedTaskExportVo> util = new ExcelUtil<>(WfFinishedTaskExportVo.class);
        util.exportExcel(response, listVo, "已办流程");
    }

    /**
     * 导出抄送流程列表
     */
    @Operation(summary = "导出抄送流程列表")
    @SaCheckPermission("workflow:process:copyExport")
    @Log(title = "抄送流程", businessType = BusinessType.EXPORT)
    @PostMapping("/copyExport")
    public void copyExport(WfCopyBo copyBo, HttpServletResponse response) {
        copyBo.setUserId(getUserId());
        List<WfCopyVo> list = copyService.selectList(copyBo);
        ExcelUtil<WfCopyVo> util = new ExcelUtil<>(WfCopyVo.class);
        util.exportExcel(response, list, "抄送流程");
    }

    /**
     * 查询流程部署关联表单信息
     *
     * @param definitionId 流程定义id
     * @param deployId     流程部署id
     */
    @Operation(summary = "查询流程部署关联表单信息")
    @GetMapping("/getProcessForm")
    @SaCheckPermission("workflow:process:start")
    public R<?> getForm(@RequestParam(value = "definitionId") String definitionId,
                        @RequestParam(value = "deployId") String deployId,
                        @RequestParam(value = "procInsId", required = false) String procInsId) {
        return R.ok(processService.selectFormContent(definitionId, deployId, procInsId));
    }

    /**
     * 根据流程定义id启动流程实例
     *
     * @param processDefId 流程定义id
     * @param variables    变量集合,json对象
     */
    @Operation(summary = "根据流程定义id启动流程实例")
    @SaCheckPermission("workflow:process:start")
    @PostMapping("/start/{processDefId}")
    public R<Void> start(@PathVariable(value = "processDefId") String processDefId, @RequestBody Map<String, Object> variables) {
        processService.startProcessByDefId(processDefId, variables);
        return R.ok(null, "流程启动成功");
    }

    /**
     * 删除流程实例
     *
     * @param instanceIds 流程实例ID串
     */
    @Operation(summary = "删除流程实例")
    @DeleteMapping("/instance/{instanceIds}")
    public R<Void> delete(@PathVariable String[] instanceIds) {
        processService.deleteProcessByIds(instanceIds);
        return R.ok();
    }

    /**
     * 读取xml文件
     *
     * @param processDefId 流程定义ID
     */
    @Operation(summary = "读取xml文件")
    @GetMapping("/bpmnXml/{processDefId}")
    public R<String> getBpmnXml(@PathVariable(value = "processDefId") String processDefId) {
        return R.ok(processService.queryBpmnXmlById(processDefId));
    }

    /**
     * 查询流程详情信息
     *
     * @param procInsId 流程实例ID
     * @param taskId    任务ID
     */
    @Operation(summary = "查询流程详情信息")
    @GetMapping("/detail")
    public R<WfDetailVo> detail(String procInsId, String taskId) {
        return R.ok(processService.queryProcessDetail(procInsId, taskId));
    }
}
